/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.andkulikov.transitionseverywhere.slice;

import com.andkulikov.transitionseverywhere.slice.util.ResUtil;
import com.transitionseverywhere.ChangeBounds;
import com.andkulikov.transitionseverywhere.ResourceTable;
import com.transitionseverywhere.Transition;
import com.transitionseverywhere.TransitionManager;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.utils.LightweightMap;

import java.util.*;

public class TransitionNamesSlice extends AbilitySlice {

    private DirectionalLayout mDlViewRoot;
    private DirectionalLayout mDlShuffleLayout;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        LayoutScatter layoutScatter = LayoutScatter.getInstance(this);
        ComponentContainer layout = (ComponentContainer) layoutScatter.
                parse(ResourceTable.Layout_slice_transition_names, null, false);
        mDlViewRoot = (DirectionalLayout) layout.findComponentById(com.andkulikov.transitionseverywhere.ResourceTable.Id_dlViewRoot);
        Object tagMap = new LightweightMap<String, ArrayList<Transition>>();
        mDlViewRoot.setTag(tagMap);

        mDlShuffleLayout = (DirectionalLayout) layout.findComponentById(ResourceTable.Id_dlShuffleLayout);
        Button btnShuffle = (Button) layout.findComponentById(ResourceTable.Id_btnShuffle);
        ShapeElement background_color_element = new ShapeElement();
        background_color_element.setShape(ShapeElement.RECTANGLE);
        background_color_element.setRgbColor(new RgbColor(ResUtil.getColor(this, ResourceTable.Color_red)));
        background_color_element.setBounds(0, 0, btnShuffle.getRight(), btnShuffle.getBottom());
        btnShuffle.setBackground(background_color_element);

        final List<String> titles = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            titles.add(String.format("Item %d", i + 1));
        }
        createViews(layoutScatter, mDlShuffleLayout, titles);
        btnShuffle.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                TransitionManager.beginDelayedTransition(layout, new ChangeBounds());
                Collections.shuffle(titles);
                createViews(layoutScatter, mDlShuffleLayout, titles);
            }
        });
        super.setUIContent(layout);

    }

    private static void createViews(LayoutScatter inflater, ComponentContainer layout, List<String> titles) {
        layout.removeAllComponents();
        for (String title : titles) {
            Text textView = (Text) inflater.parse(ResourceTable.Layout_slice_names_item, layout, false);
            textView.setText(title);
            TransitionManager.setTransitionName(textView, title);
            layout.addComponent(textView);
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

}
